package rta.datos.conexion
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	
	import rta.datos.to.Carpeta;

	public class CarpetaDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		
		public function CarpetaDB(conn:SQLConnection)
		{
			this.conn = conn;
			statement = new SQLStatement();
			statement.itemClass = Carpeta;
			statement.sqlConnection = conn;
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		//-------------------------------------------------
		
		
		//--OBTENER CARPERTAS IMAGENES-----------
		public function obtenerCarpetasImagenes():void{					
			statement.text = "SELECT DISTINCT c.id_carpeta id, " +
							 "c.nombre nombre " +
							 "FROM imagen i " +
							 "INNER JOIN carpeta c ON c.id_carpeta = i.id_carpeta " +
							 "WHERE i.id_carpeta is not null";
			statement.addEventListener(SQLEvent.RESULT, resultObtenerCarpetasImagenes);
			statement.execute();
		}
		
		private function resultObtenerCarpetasImagenes(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerCarpetasImagenes);
			var result:SQLResult = statement.getResult();
			if (result != null && this._callback != null) {  
				this._callback(new ArrayCollection(result.data));
			}
		}
		//-------------------------------------
		
		//--OBTENER CARPERTAS SONIDOS----------
		public function obtenerCarpetasSonidos():void{					
			statement.text = "SELECT DISTINCT c.id_carpeta id, " +
							 "c.nombre nombre " +
							 "FROM sonido s " +
							 "INNER JOIN carpeta c ON c.id_carpeta = s.id_carpeta " +
							 "WHERE s.id_carpeta is not null";
			statement.addEventListener(SQLEvent.RESULT, resultObtenerCarpetasSonidos);
			statement.execute();
		}
		
		private function resultObtenerCarpetasSonidos(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerCarpetasSonidos);
			var result:SQLResult = statement.getResult();
			if (result != null && this._callback != null) {  
				this._callback(new ArrayCollection(result.data));
			}
		}
		//-------------------------------------
	}
}